<?php 

/**
 *  数据分页类
    date     2016-09-25
    author : YWH
 */
namespace Think;
class Mypage{

  	public $totalRec    = 0;

  	public $recPerPage  = 1;

  	public $pram        = 2;

    private $prevPage;

    private $nextPage;

    private $numPage;

    private $limit;

    private $resArr;

    private $totalPage;

    public function __construct($totalRec, $recPerPage, $param){//总记录条数 每页显示条数 每页显示数字页码个数

     
      if ($totalRec) {//参数未传入 则赋值默认参数 有参数传入 则将参数赋值
        
         $this->totalRec  = $totalRec;

      }

    
    	  //$recPerPageh是否传参
    	  if ($recPerPage) {
    	  		
    	  	$this->recPerPage = $recPerPage;

    	  }

    	   if ($param) {
    	  		
    	  	$this->param = $param;

    	    }

    }


         public function __get($param1){//get方法获取参数

            // var_dump($param1);

            return $this->$param1;

         }

         public function pgscript(){//添加js脚本 第一页最后一页 隐藏上一页下一页

               $script = '';
               $script="<script>window.onload = function(){
                  var cpg = document.querySelector('.pageNow');
                  var firstpg = document.querySelectorAll('.page')[0];
                  var prepg = document.querySelectorAll('.page')[1];
                  var nexpg = document.querySelectorAll('.page')[2];
                  var lastpg = document.querySelectorAll('.page')[3];

                  var tsSpan = document.getElementById('ts');

                  function b(){
              
                  	tsSpan.style.display = 'none';

                  }

                  if(cpg.innerHTML == 1){
                    prepg.style.display= 'none';

                   	firstpg.style.display= 'none';

                         tsSpan.innerHTML = '已经是第一页了';
                     

                         setTimeout(b,2000);
                        

                  

                  }else if (cpg.innerHTML ==".$this -> totalPage."){
                    nexpg.style.display= 'none';

                     lastpg.style.display= 'none';

                     tsSpan.innerHTML = '已经是最后一页了';
                      setTimeout(b,2000);
                  }
                 }</script>";

                 echo $script;

                 return;
         }	

    	  public  function getPage(){

    	  	//总页数

    	  	$this->totalPage =ceil($this->totalRec/$this->recPerPage);

 
              $url = $_SERVER['REQUEST_URI'];

              $arr = parse_url($url);
              // var_dump($arr);

              if(!empty($arr['query'])){

                 parse_str($arr['query'],$myquery);

                 unset($myquery['page']);

                 $url = empty($myquery) ? $arr['path'].'?': $arr['path'].'?'.http_build_query($myquery).'&'; 

              }else{

                 $url=$arr['path'].'?';


              }

                //  var_dump($arr['query']);
                //  echo "<br>";
                // var_dump($myquery);

                  if(empty($_GET['page'])){
                     $nowPage = 1;
                    }else{

                        $nowPage = $_GET['page'];

                    }
                    if( $nowPage <= 0){

                         $nowPage = 1;

                    }
                    if( $nowPage >= $this->totalPage){

                        $nowPage = $this->totalPage;

                    }
  


                $offset = $this->recPerPage * ($nowPage - 1);

              
              
                $num = $this->param <$this->totalPage? $this->param:$this->totalPage;//显示页码个数

                $start=0;//开始页码
                
                $end=0;//末尾页码


                if($nowPage>$this->totalPage){//当前页大于总页数 当前页=最后一页
                  
                  $nowPage=$this->totalPage;

                }

                $nums1=intval($num/2);//开始项当前的个数
               
                $nums2=$num%2==0?$nums1-1:$nums1;//末尾项当前的个数 判断是偶数还是奇数,是偶数就减1

                if($nowPage<=$num-$nums2) //当前页数小于或等于显示页码减去末尾项,当前位置还处于页码范围
                {
                    $start=1;

                    $end=$num;

                }else
                { 
                      $start=$nowPage-$nums1;

                      $end=$nowPage+$nums2;  

                }

                // echo $nowPage.'<br/>';
                // echo $num-$nums2;

                


                /*当计算出来的末尾项大于总页数*/
                if($end>$this->totalPage)
                {   
                  // if ($start < $num ) {
                  //   # code...
                  // }
                    $start=($this->totalPage-$num)+1;//开始项等于总页数减去要显示的数量然后再自身加1
                     
                      $end=$this->totalPage;
                }
                  $this->numPage='';

                for($i= $start;$i <= $end;$i++)
                {
                  if($nowPage==$i)
                  {
                    
                     $this->numPage.="<a class = 'pageNow numPage' href='".$url."page=".$i."'>".$i."</a> ";
                 
                  }else{

                    $this->numPage.="<a class = 'pageNum numPage' href='".$url."page=".$i."'>".$i."</a> ";

                  }

                  

                  
                }
				         
                 $first = 1;
                 
                 $pre  = $nowPage -1 <= 0 ? 1: $nowPage -1;

                 $next = $nowPage +1 >= $this->totalPage ? $this->totalPage:$nowPage+1;
                 
                 $this->prevPage="<a class = 'page' href='".$url."page=".$pre."'>上一页</a> ";
                 
                 $this->nextPage= "<a class = 'page' href='".$url."page=".$next."'> 下一页</a>";

                 $this->firstPage="<a class = 'page' href='".$url."page=".$first."'>首页</a> ";

                 $this->lastPage="<a class = 'page aa' href='".$url."page=".$this->totalPage."'>尾页</a><span id = 'ts'></span> ";

                 $this->numPage =$this->firstPage.$this->prevPage.$this->numPage .$this->nextPage.$this->lastPage;

                 $this->limit = "$offset,$this->recPerPage";

                 $this->resArr = array();

                 $this->resArr = array('limit'=>$this->limit,'nowPage'=>$nowPage,'totalPage'=>$this->totalPage,'numPage'=>$this->numPage);
                
                 $this->pgscript();

                 return $this;
            }

}

?>
          

<!doctype html>
 <html lang="en">
 <head>
  <meta charset="UTF-8">
  <title>Document</title>
  <script></script>
  <style>
    .page{
      
      display: inline-block;

      background-color: #58AEF9;

      width: 60px;

      height: 24px;

      text-align: center;

      line-height: 24px;

      text-decoration: none;







    }

    .aa{
      margin-left:5px;
    }
    .pageNum{

      display: inline-block;

      background-color: #58AEF9;

      width: 30px;

      height: 24px;

      text-align: center;

      line-height: 24px;

      text-decoration: none;

      color:#FFCC33;

    }


    a:hover{

      background-color: #66CCCC;

      color: white;
    }


    .pageNow{

      display: inline-block;

      background-color: green;

      width: 30px;

      height: 24px;

      text-align: center;

      line-height: 24px;

      text-decoration: none;

      color: white;



    }


#ts{
  
  color: red;

  margin-left: 5px;


}



</style>



 </head>
 <body>
  
 </body>
 </html>